<?php
include("include/config.inc.php");

$pageName = 'furnaceReport';
if($_SESSION['s_userType'] == 'User')
{
   $authorization = "NoAccess";
   foreach($selUsermenuArr as $value)
   {
     if(in_array($pageName, $value))
       {
         $authorization ="Access";
         break;
       }
       else 
       {
        $authorization = "NoAccess";    
       }
   }
   if($authorization == "NoAccess")
   {
     header("location:index.php");
   }
}
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$dataArray = 0;
	$selFId    = 0;
	$orderBy   = 'DESC';
	$fromDate  = date('Y-m-d');
	$toDate    = date('Y-m-d');
	if(isset($_POST['fId']))
  {
   	$selFId   = $_POST['fId'];
    $fromDate = $_POST['fromDateYear']."-".$_POST['fromDateMonth']."-".$_POST['fromDateDay'];
	  $toDate   = $_POST['toDateYear']."-".$_POST['toDateMonth']."-".$_POST['toDateDay'];
  	
    $selFurnace = "SELECT fId,furnaceName
  	                 FROM furnace
  	                WHERE 1 = 1 ";
  	if(isset($_POST['fId']) && $_POST['fId'] > 0)
	  {
      $selFurnace .= " AND fId = ".$_POST['fId'];
    }
    $selFurnace .= " ORDER BY furnaceName";
    
    $selFurnaceRes = mysql_query($selFurnace);
    $count = 0;
    $dataArray = array();
    while($furnaceRow = mysql_fetch_array($selFurnaceRes))
    {
    	$selPlanning = "SELECT planningId,fId1,fId2,fId3
    	                  FROM planning
    	                 WHERE 1 = 1 ";
      $selPlanning .= "  AND (planning.fId1 = ".$furnaceRow['fId']." || planning.fId2 = ".$furnaceRow['fId']." || 
                             planning.fId3 = ".$furnaceRow['fId'].")";
      
      $selPlanningRes = mysql_query($selPlanning);
      while($mainRow = mysql_fetch_array($selPlanningRes))
      {
      	$processNo = 0;
       	if($furnaceRow['fId'] == $mainRow['fId1'])
      	{
      	  $processNo = 1;
      	}
      	if($furnaceRow['fId'] == $mainRow['fId2'])
      	{
      	  $processNo = 2;
      	}
      	if($furnaceRow['fId'] == $mainRow['fId3'])
      	{
          $processNo = 3;
        }
        
        if($processNo > 0)
        {
          $selFurData = "SELECT logSheetId,logsheet.planningId,logsheetNo,
    	                          DATE_FORMAT(logsheetDate,'%d-%m-%y') AS logsheetDate,
    	                          DATE_FORMAT(startTime,'%H:%i') AS startTime,
    	                          DATE_FORMAT(endTime,'%H:%i') AS endTime,logsheet.status,notOkReason,challan,hardne,partyName,materialName,
    	                          furnace1.furnaceName AS fName1,furnace2.furnaceName AS fName2,
    	                          furnace3.furnaceName AS fName3, pr1.processName AS process1,
    	                          pr2.processName AS process2, pr3.processName AS process3,temp1,soaking1,temp2,soaking2,
    	                          temp3,soaking3,quantity
    	                     FROM logsheet
    	                     JOIN incoming ON logsheet.incomingId = incoming.incomingId
    	                     JOIN party ON party.partyId = incoming.partyId
    	                     JOIN material ON incoming.materialId = material.materialId
    	                     JOIN planning ON logsheet.planningId = planning.planningId
                           LEFT JOIN process AS pr1 ON planning.processId1 = pr1.processId
                           LEFT JOIN process AS pr2 ON planning.processId2 = pr2.processId
                           LEFT JOIN process AS pr3 ON planning.processId3 = pr3.processId
                           LEFT JOIN furnace AS furnace1 ON planning.fId1 = furnace1.fId
                           LEFT JOIN furnace AS furnace2 ON planning.fId2 = furnace2.fId
                           LEFT JOIN furnace AS furnace3 ON planning.fId3 = furnace3.fId
                          WHERE logsheetDate >= '".$fromDate."'
                            AND logsheetDate <= '".$toDate."'
                            AND processNo = ".$processNo."
                            AND logsheet.planningId = ".$mainRow['planningId']."
                            GROUP BY materialName
                            ORDER BY fName1,fName2,fName3 ASC";
          $selFurDataRes = mysql_query($selFurData);
          while($logRow = mysql_fetch_array($selFurDataRes))
          {
          	$dataArray[$count]['logSheetId']   = $logRow['logSheetId'];
          	$dataArray[$count]['planningId']   = $logRow['planningId'];
          	$dataArray[$count]['logsheetNo']   = $logRow['logsheetNo'];
          	$dataArray[$count]['logsheetDate'] = $logRow['logsheetDate'];
          	$dataArray[$count]['startTime']    = $logRow['startTime'];
          	$dataArray[$count]['endTime']      = $logRow['endTime'];
          	$dataArray[$count]['notOkReason']  = $logRow['notOkReason'];
          	$dataArray[$count]['challan']      = $logRow['challan'];
          	$dataArray[$count]['hardne']       = $logRow['hardne'];
          	$dataArray[$count]['partyName']    = $logRow['partyName'];
          	$dataArray[$count]['materialName'] = $logRow['materialName'];
          	
          	if($logRow['status'] == 'outgoing' || $logRow['status'] == 1)
          	{
          		$dataArray[$count]['status']     =  'Ok'; 
          	}
          	else if($logRow['status'] == 0)
          	{
          		$dataArray[$count]['status']     =  'Not Ok'; 
          	}
          	else
          	{
          		$dataArray[$count]['status']     =  'N D'; 
          	}
          
          
  	       	if($furnaceRow['fId'] == $mainRow['fId1'])
  	      	{
  	      	  $dataArray[$count]['furnace'] = $logRow['fName1'];
  	      	  $dataArray[$count]['process'] = $logRow['process1'];
  	      	  $dataArray[$count]['temp']    = $logRow['temp1'];
  	      	  $dataArray[$count]['soaking'] = $logRow['soaking1'];
  	      	}
  	      	if($furnaceRow['fId'] == $mainRow['fId2'])
  	      	{
  	      	  $dataArray[$count]['furnace'] = $logRow['fName2'];
  	      	  $dataArray[$count]['process'] = $logRow['process2'];
  	      	  $dataArray[$count]['temp']    = $logRow['temp2'];
  	      	  $dataArray[$count]['soaking'] = $logRow['soaking2'];
  	      	}
  	      	if($furnaceRow['fId'] == $mainRow['fId3'])
  	      	{
  	          $dataArray[$count]['furnace'] = $logRow['fName3'];
  	          $dataArray[$count]['process'] = $logRow['process3'];
  	          $dataArray[$count]['temp']    = $logRow['temp3'];
  	          $dataArray[$count]['soaking'] = $logRow['soaking3'];
  	        }
  	        
          	$orderBy = $_POST['orderBy'];
          	
          	$dataArray[$count]['quantity'] = $logRow['quantity'];
          	$count++;
          }
        }
      }   
    }
	}
	$a = 0;
	$furnaceArray = array();
	$selectFurnace = "SELECT fId,furnaceName
	                    FROM furnace
	                   ORDER BY furnaceName";
	$selectFurnaceResult = mysql_query($selectFurnace);
	while($row = mysql_fetch_array($selectFurnaceResult))
	{
	  $furnaceArray['fId'][$a]   = $row['fId'];
	  $furnaceArray['furnaceName'][$a] = $row['furnaceName'];
	  $a++;
	}
	include("./bottom.php");
	$smarty->assign("furnaceArray",$furnaceArray);
	$smarty->assign("dataArray",$dataArray);
	$smarty->assign("selFId",$selFId);
	$smarty->assign("orderBy",$orderBy);
	$smarty->assign("fromDate",$fromDate);
	$smarty->assign("toDate",$toDate);
  $smarty->display("furnaceReport.tpl");
}
?>